package com.szewec.ps.ui.road.facilities.investigation.task.roadside.facilities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.R;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.Enums;
import com.szewec.ps.databinding.FragmentRoadTrafficSignBinding;
import com.szewec.ps.model.road.facilities.investigation.road.side.facilities.RoadTrafficSign;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTask;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTaskDTO;
import com.szewec.ps.model.selectmappoint.SelectedPoint;
import com.szewec.ps.util.RollUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.TextViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 交通标志
 */
public class RoadTrafficSignFragment extends Fragment {
    private FragmentRoadTrafficSignBinding binding;

    // 路段设施调查任务DTO
    private RoadFacilitiesInvestigationTaskDTO.RoadFacilitiesTaskDTO roadFacilitiesTaskDTO;
    private RoadTrafficSign roadTrafficSign;
    // 标志类型 数据字典
    private List<String> signTypeDict = new ArrayList<>();
    // 标志类型-二级标志 数据字典
    private List<String> signTypeChildDict = new ArrayList<>();
    // 坐标
    private String selectedPoint;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_road_traffic_sign, container, false);
        // 2、获取到视图
        View inflate = binding.getRoot();
        ButterKnife.bind(this, inflate);

        initData();
        initView();
        return inflate;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 通过Activity中的public方法来获取值
        roadFacilitiesTaskDTO = ((RoadsideFacilitiesActivity) context).pushFragmentData();
    }

    private void initData() {
        signTypeDict = Enums.TrafficSignTypeEnum.getEnumValueList();
        roadTrafficSign = roadFacilitiesTaskDTO.getRoadTrafficSign() == null ? new RoadTrafficSign() : roadFacilitiesTaskDTO.getRoadTrafficSign();
    }

    private void initView() {
        // 获取坐标
        SelectedPoint selectedPoint = JSONObject.parseObject(roadTrafficSign.getCoordinate(), SelectedPoint.class);
        selectedPoint = selectedPoint == null ? new SelectedPoint() : selectedPoint;
        // 标志类型
        Integer signType = roadTrafficSign.getSignType();
        // 二级标志
        Integer secondarySignType = roadTrafficSign.getSecondarySignType();

        binding.coordinate.setValue(selectedPoint.getxCoordinate(), selectedPoint.getyCoordinate());
        binding.signType.setValue(Enums.TrafficSignTypeEnum.getValueByKey(signType));

        // 如果二级标志不为空 展示二级标志内容
        if (signType != null && secondarySignType != null) {
            initSecondarySignType(Enums.TrafficSignTypeEnum.getValueByKey(signType), Enums.TrafficSignChildTypeEnum.getValueByKey(secondarySignType));
        }
    }

    public RoadTrafficSign getData() {
        // 添加路段调查任务ID
        RoadFacilitiesInvestigationTask facilitiesInvestigationTask = roadFacilitiesTaskDTO.getFacilitiesInvestigationTask();
        if (facilitiesInvestigationTask != null && StringUtils.isNotEmpty(facilitiesInvestigationTask.getId())) {
            roadTrafficSign.setRoadInvestigationTaskId(facilitiesInvestigationTask.getId());
        }
        roadTrafficSign.setCoordinate(selectedPoint);
        roadTrafficSign.setSignType(Enums.TrafficSignTypeEnum.getKeyByValue(binding.signType.getStringValue()));
        roadTrafficSign.setSecondarySignType(Enums.TrafficSignChildTypeEnum.getKeyByValue(binding.secondarySignType.getStringValue()));

        return roadTrafficSign;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SelectedPoint centerPoint = null;
        //选择了坐标
        if (resultCode == Constants.RESULT_CODE.SELECTED_POINT) {
            if (data != null) {
                Bundle presentValue = data.getExtras();
                centerPoint = (SelectedPoint) presentValue.getSerializable(Constants.EXTRA_KEY.MAP_CENTER_POINT);
            }
        }
        if (centerPoint == null) {
            return;
        }
        selectedPoint = JSONObject.toJSONString(centerPoint);
    }

    @OnClick({R.id.signType, R.id.secondarySignType})
    public void onViewClicked(View view) {
        //隐藏系统软键盘
        TextViewUtil.closeKeyboard(getActivity());
        switch (view.getId()) {
            case R.id.signType:
                RollUtil.alertBottomWheelOption(getContext(), signTypeDict, binding.signType.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        String signTypeStr = signTypeDict.get(index);
                        binding.signType.setValue(signTypeStr);
                        initSecondarySignType(signTypeStr, "");
                    }
                });
                break;
            case R.id.secondarySignType:
                RollUtil.alertBottomWheelOption(getContext(), signTypeChildDict, binding.secondarySignType.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.secondarySignType.setValue(signTypeChildDict.get(index));
                    }
                });
        }
    }

    /**
     * 填充二级标志内容
     *
     * @param signTypeStr          父节点选中的值
     * @param secondarySignTypeStr 二级菜单选中的值
     */
    private void initSecondarySignType(String signTypeStr, String secondarySignTypeStr) {
        // 根据选择的标志类型 动态填充二级标志里面的选项
        // 初始化二级标志内容
        signTypeChildDict = new ArrayList<>();
        binding.secondarySignType.setValue(secondarySignTypeStr);
        // 获取父节点的key
        Integer signTypeKey = Enums.TrafficSignTypeEnum.getKeyByValue(signTypeStr);
        // 根据父节点的key 获取二级标志类型List
        signTypeChildDict = Enums.TrafficSignChildTypeEnum.getValueListByParentKey(signTypeKey);
        // 填充二级标志title
        binding.textTitle.setText(signTypeStr);
        binding.secondarySignType.setItemName(signTypeStr);
        // 显示二级标志内容
        binding.llContainerSign.setVisibility(View.VISIBLE);
    }
}